﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;



namespace ProjektXNA
{
    class Level : GameComponent // Ärver från Gamecomponent
    {
        public int TileWidth { get; set; } //Brickornas storlek
        public int TileHeight { get; set; } //Brickornas storlek
        public int[,] Data { get; set; } //Datan - kartan
        public Texture2D TileMap { get; set; } // Grafik
        public Vector2 CameraPosition { get; set; } // Position på kartan

        private int viewportWidth, viewportHeight; //Storleken på skärmen

        public override void Initialize()
        {
            viewportWidth = Game.GraphicsDevice.Viewport.Width;
            viewportHeight = Game.GraphicsDevice.Viewport.Height;

            TileWidth = 32;
            TileHeight = 32;

            Data = new int[,]
            {
              {0,0,1,0,0,0,0,0},
              {0,0,1,1,0,0,0,0},
              {0,0,0,1,1,0,0,0},
              {0,0,0,0,1,0,0,0},
              {0,0,0,1,1,0,0,0},
              {0,0,1,1,0,0,0,0},
              {0,0,1,0,0,0,0,0},
              {0,0,1,0,0,0,0,0}
            };

            base.Initialize();
        }

        public Level(Game game)
            : base(game)
        {
            game.Components.Add(this);
            //CameraPosition = Vector2.Zero;
        }

        public void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            if (Data == null || TileMap == null)
                return;
            //Kameran
            //int screenCenterX = 0; viewportWidth / 2;
            //int screenCenterY = 0; viewportHeight / 2;

            //Vart vi börjar rita
            int startX = 0;//(int)((CameraPosition.X - screenCenterX) / TileWidth);
            int startY = 0;//(int)((CameraPosition.Y - screenCenterY) / TileHeight);

            //Vart sista tilen skall vara
            int endX = (startX + viewportWidth / TileWidth);
            int endY = (startY + viewportHeight / TileHeight);

            Vector2 position = Vector2.Zero;
            int tilesPerLine = TileMap.Width / TileWidth; //64 / 32 = 2, vi har 2 tiles

            for (int y = startY; y < Data.GetLength(0) && y <= endY; y++)
            {
                for (int x = startX; x < Data.GetLength(1) && x <= endX; x++)
                {
                    //Om man vill ha en kamera som rör sig
                    //position.X = (x * TileWidth - CameraPosition.X + screenCenterX); 
                    //position.Y = (y * TileHeight - CameraPosition.Y + screenCenterY);
                    position.X = (x * TileWidth);
                    position.Y = (y * TileHeight);

                    int index = Data[y, x];
                    Rectangle tileGfx = new Rectangle((index % tilesPerLine) * TileWidth,
                        (index / tilesPerLine) * TileHeight, TileWidth, TileHeight);

                    spriteBatch.Draw(TileMap, position, tileGfx, Color.White, 0, Vector2.Zero, 1.0f, SpriteEffects.None, 0);
                }
            }
        }

        public int getIndex(int cellX, int cellY)
        {
            if (cellX < 0 || cellX > Data.GetLength(1) || cellY < 0 || cellY > Data.GetLength(0))
                return 0;

            return Data[cellY, cellX];
        }
    }
}
